package com.example.costumview.customview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.costumview.R;


public class GetPosTanView extends View {
    private static final String TAG = "GetPosTanView";
    private Path mCirclePath, mDstPath;
    private Paint mPaint;
    private PathMeasure mPathMeasure;
    private Float mCurAnimValue;
    private Bitmap mArrawBmp;

    public GetPosTanView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mArrawBmp = BitmapFactory.decodeResource(getResources(), R.drawable.arraw);

        mPaint=new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(8);

        mCirclePath = new Path();
        mDstPath = new Path();

        mCirclePath.addCircle(200,200,150, Path.Direction.CW);

        mPathMeasure = new PathMeasure(mCirclePath,true);

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                mCurAnimValue = (Float) animation.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.setDuration(2000);
        valueAnimator.start();
    }

    private float[] pos=new float[2];
    private float[] tan=new float[2];

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        float length = mPathMeasure.getLength();

        float stop=mCurAnimValue*length;


        mDstPath.reset();
        mPathMeasure.getSegment(0,stop,mDstPath,true);


        canvas.drawPath(mDstPath,mPaint);


        mPathMeasure.getPosTan(stop,pos,tan);

        Log.e(TAG, "onDraw:pos== "+pos[0]+"::"+pos[1] );
        Log.e(TAG, "onDraw:tan== "+tan[0]+"::"+tan[1] );

//        float degrees = (float) (Math.atan2(tan[1], tan[0]) * 180.0 / Math.PI);
//        Matrix matrix=new Matrix();
//        matrix.postRotate(degrees,mArrawBmp.getWidth()/2,mArrawBmp.getHeight()/2);
//        matrix.postTranslate(pos[0]-mArrawBmp.getWidth()/2,pos[1]-mArrawBmp.getHeight()/2);
//        canvas.drawBitmap(mArrawBmp,matrix,mPaint);

        //计算方位角
        Matrix matrix=new Matrix();
        mPathMeasure.getMatrix(stop,matrix,PathMeasure.POSITION_MATRIX_FLAG|PathMeasure.TANGENT_MATRIX_FLAG);
        matrix.preTranslate(-mArrawBmp.getWidth()/2,-mArrawBmp.getHeight()/2);
        canvas.drawBitmap(mArrawBmp,matrix,mPaint);





    }
}
